package com.sinodata.bsm.cicp.finder.aix;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.util.CommandReader;

/**
 * Description: AIX卷组发现程序
 * 程序返回：
 *  1 物理分区大小
 *  2 访问权限
 *  3 物理分区数量
 *  4 卷组状态（unavailable/available）
 *  5 卷组名称
 */
public class VolumeGroup extends AixFinder {

    private static final Logger logger = Logger.getLogger(VolumeGroup.class);
    private static final String SHELL = "lsvg";

    @SuppressWarnings("unchecked")
    @Override
    public List<String[]> find(String ip, Map params) {
        List<String[]> list = new ArrayList<String[]>();
        String[] volumeGroupRes = null;
        BufferedReader reader = null;
        String strLine = null;
        try {
            reader = CommandReader.getReader(ip, SHELL, 0, 0);
            List<String> groups = new ArrayList<String>();
            while ((strLine = reader.readLine()) != null) {
                if (strLine.length() == 0) {
                    continue;
                }
                groups.add(strLine.trim());
            }
            if (groups.size() > 0) {
                for (String str : groups) {
                    String cmdStr = SHELL + " " + str;
                    reader = CommandReader.getReader(ip, cmdStr, 0, 0);
                    strLine = reader.readLine();
                    volumeGroupRes = new String[9];
                    list.add(volumeGroupRes);
                    volumeGroupRes[0] = str;
                    volumeGroupRes[1] = str;
                    volumeGroupRes[2] = getHostName(ip) + str;
                    volumeGroupRes[8] = str; //卷组名称
                    if (strLine == null || strLine.indexOf(str) == -1) {
                        volumeGroupRes[3] = "0"; //物理分区大小
                        volumeGroupRes[4] = "N/A"; //访问权限
                        volumeGroupRes[5] = "0"; //物理分区数量
                        volumeGroupRes[6] = "unavailable"; //卷组状态
                        volumeGroupRes[7] = "0"; //总物理分区大小
                    } else {
                        volumeGroupRes[6] = "available"; //卷组状态
                        str = reader.readLine();//第二行
                        //PP大小(MB)
                        String ppsSize = "0";
                        int ppsSizeInt = 0;
                        if (str != null) {
                            ppsSize = str.substring(str.indexOf("PP SIZE:") + "PP SIZE:".length(), str.indexOf("megabyte"));
                        }
                        try {
                            ppsSizeInt = Integer.parseInt(ppsSize);
                        } catch (Exception e) {
                            ppsSizeInt = 0;
                        }
                        volumeGroupRes[3] = ppsSize;
                        //权限(VG PERMISSION:) TOTAL PPs:
                        String tmpStr = reader.readLine();//第三行
                        if (tmpStr == null) {
                            tmpStr = "";
                        }
                        str = tmpStr.substring(tmpStr.indexOf("VG PERMISSION:") + "VG PERMISSION:".length(), tmpStr.indexOf("TOTAL PPs:"));
                        String permStr = str.trim();
                        volumeGroupRes[4] = permStr;
                        //总PP数量(个)
                        str = tmpStr.substring(tmpStr.indexOf("TOTAL PPs:") + "TOTAL PPs:".length(), tmpStr.indexOf("("));
                        String ppsStr = str.trim();//第四行
                        int ppsInt = 0;
                        try {
                            ppsInt = Integer.parseInt(ppsStr);
                        } catch (Throwable t) {
                            t.getMessage();
                            ppsInt = 0;
                        }
                        volumeGroupRes[5] = ppsInt + "";
                        //总物理分区大小
                        volumeGroupRes[7] = "" + (ppsInt * ppsSizeInt);
                    }
                }
            }
            return list;
        } catch (IOException e) {
            logger.error("failed to find aix volume group", e);
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    logger.error("failed to close reader", e);
                }
            }
        }
        return null;
    }
}
